{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "     销量\n货号     \naaa   2\nbbb   3",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n    </tr>\n    <tr>\n      <th>货号</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>aaa</th>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>bbb</th>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.read_excel(\n",
    "    'on_index.xlsx',\n",
    "    index_col=[0] # 货号设置成索引\n",
    ")\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "     库存\n货号     \naaa  10\nbbb  20\nccc  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>库存</th>\n    </tr>\n    <tr>\n      <th>货号</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>aaa</th>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>bbb</th>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>ccc</th>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.read_excel(\n",
    "    'on_index.xlsx',\n",
    "    sheet_name=1, # 读取表格2\n",
    "    index_col=[0]\n",
    ")\n",
    "df2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "      销量  库存\n货号          \naaa  2.0  10\nbbb  3.0  20\nccc  NaN  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n      <th>库存</th>\n    </tr>\n    <tr>\n      <th>货号</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>aaa</th>\n      <td>2.0</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>bbb</th>\n      <td>3.0</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>ccc</th>\n      <td>NaN</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过索引连接\n",
    "pd.merge(df1, df2,\n",
    "         left_index=True,\n",
    "         right_index=True,\n",
    "         how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "      销量  库存\n货号          \naaa  2.0  10\nbbb  3.0  20\nccc  NaN  30",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n      <th>库存</th>\n    </tr>\n    <tr>\n      <th>货号</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>aaa</th>\n      <td>2.0</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>bbb</th>\n      <td>3.0</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>ccc</th>\n      <td>NaN</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 和上面的方式相同\n",
    "df1.join(df2, how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "     年份  月份  总销量\n0  2019   1   10\n1  2019   2   20\n2  2020   1   30\n3  2020   2   40",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年份</th>\n      <th>月份</th>\n      <th>总销量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2019</td>\n      <td>1</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2019</td>\n      <td>2</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2020</td>\n      <td>1</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2020</td>\n      <td>2</td>\n      <td>40</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = pd.read_excel(\n",
    "    'on_list.xlsx'\n",
    ")\n",
    "df3"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "     年份  月份  总收入\n0  2020   1  300\n1  2020   2  400\n2  2019   1  100\n3  2019   2  200",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年份</th>\n      <th>月份</th>\n      <th>总收入</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2020</td>\n      <td>1</td>\n      <td>300</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2020</td>\n      <td>2</td>\n      <td>400</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2019</td>\n      <td>1</td>\n      <td>100</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2019</td>\n      <td>2</td>\n      <td>200</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = pd.read_excel(\n",
    "    'on_list.xlsx',\n",
    "    sheet_name=1\n",
    ")\n",
    "df4"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "     年份  月份  总销量  总收入\n0  2019   1   10  100\n1  2019   2   20  200\n2  2020   1   30  300\n3  2020   2   40  400",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年份</th>\n      <th>月份</th>\n      <th>总销量</th>\n      <th>总收入</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2019</td>\n      <td>1</td>\n      <td>10</td>\n      <td>100</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2019</td>\n      <td>2</td>\n      <td>20</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2020</td>\n      <td>1</td>\n      <td>30</td>\n      <td>300</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2020</td>\n      <td>2</td>\n      <td>40</td>\n      <td>400</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过list连接（on参数）\n",
    "pd.merge(df3, df4,on=['年份', '月份'])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}